/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.domain.KwOccupy;
import me.zhengjie.kwService.OccupyService;
import me.zhengjie.pojo.dto.OccupyDto;
import me.zhengjie.pojo.vo.OccupyVo;
import me.zhengjie.service.KwOccupyService;
import me.zhengjie.service.dto.KwOccupyQueryCriteria;
import me.zhengjie.util.BaseResponse;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
* @website https://el-admin.vip
* @author LongYang
* @date 2021-06-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "考试占用情况、监考老师和教室表前端控制器管理")
@RequestMapping("/api/kwOccupy")
public class KwOccupyController {

    private final KwOccupyService kwOccupyService;
    private final OccupyService occupyService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('kwOccupy:list')")
    public void download(HttpServletResponse response, KwOccupyQueryCriteria criteria) throws IOException {
        kwOccupyService.download(kwOccupyService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询考试占用情况、监考老师和教室表前端控制器")
    @ApiOperation("查询考试占用情况、监考老师和教室表前端控制器")
    @PreAuthorize("@el.check('kwOccupy:list')")
    public ResponseEntity<Object> query(KwOccupyQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(kwOccupyService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增考试占用情况、监考老师和教室表前端控制器")
    @ApiOperation("新增考试占用情况、监考老师和教室表前端控制器")
    @PreAuthorize("@el.check('kwOccupy:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody KwOccupy resources){
        return new ResponseEntity<>(kwOccupyService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改考试占用情况、监考老师和教室表前端控制器")
    @ApiOperation("修改考试占用情况、监考老师和教室表前端控制器")
    @PreAuthorize("@el.check('kwOccupy:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody KwOccupy resources){
        kwOccupyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除考试占用情况、监考老师和教室表前端控制器")
    @ApiOperation("删除考试占用情况、监考老师和教室表前端控制器")
    @PreAuthorize("@el.check('kwOccupy:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        kwOccupyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /*-------------------------------------------------------------------------------*/
    @ApiOperation("考试占用情况，监考老师或教室查询")
    @PostMapping("/get-kwOccupy")
    @PreAuthorize("@el.check('kwOccupy:getKwOccupy')")
    public BaseResponse getKwOccupy(@RequestBody @Valid OccupyDto dto){
        try {
            List<OccupyVo> list = occupyService.findAll(dto);
            return BaseResponse.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.failed("查询失败："+e.getMessage());
        }
    }
}